<?php
/**
 *  date: 2017/7/10  11:03
 *  Author: Lin
 *  encoding:utf-8  language:PHP
 */
namespace Home\Controller;
use Home\Model\ServeModel;
use Home\Model\UsersModel;


class ServeController extends BaseController
{
    protected $_serveObject;

    public function _initialize()
    {
        parent::_initialize();
        $this->_serveObject = new ServeModel();
    }


/**********************************************服务商start************************************************/
    /**
     *服务申请列表
     * @Date 201707
     * @Author Lin
     */
    public function proposerList()
    {
       $name =  I('facilitator_name');
       $mobile =  I('mobile');
       $status =  I('status');
       $type =  I('type');
        $data = $this->_serveObject->getproposerList($name,$mobile,$status,$type);
        $data = $this->_serveObject->getproposerList($name,$mobile,$status,$type);

//        var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    /**
     *增加
     * @Date 201707
     * @Author Lin
     */
    public function proposerAdd()
    {
        $fpModel = M('facilitator_proposer');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                    $ret = uploadOne('image', 'Proposer', array(array(300, 300)));
                    if($ret['ok']==1)
                    {
                        $dataPost['image'] = $ret['images'][0];
                        $dataPost['smallimage'] = $ret['images'][1];
                    }
                    $dataPost['add_time'] = time();
                    //var_dump($dataPost);die;
                    if(false !== $fpModel->add($dataPost))
                    {
                        $this->success('增加成功');
                        exit;
                    }
            }
            $this->error($fpModel->getError());
        }
        $this -> display();
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function proposerEdit()
    {
        $id = I('id');
        $fpModel = M('facilitator_proposer');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($fpModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'Proposer', array(array(300, 300)));

                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $fpModel->save($dataPost))
                {
                    $this->success('修改成功');
                    exit;
                }
            }
            $this->error($fpModel->getError());
        }
        $data = $fpModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }
    /**********************************************服务商end************************************************/


    /**********************************************酒店start************************************************/
    /**
     酒店申请表
     * @Date 201707
     * @Author Lin
     */
    public function hotelList()
    {
        $this->display('hotel_list');
    }

    /**
     *总页数
     * @Date 201707
     * @Author Lin
     */
    public function getHotelPageCount()
    {
        $map['status'] = 2;
        $count = M('hotel') -> where($map) -> count();
        $Page = ceil($count/5);
        $this->ajaxReturn(array('num' => $Page));
    }

    /**
     *
     * @Date 201707
     * @Author Lin
     */
    public function ajax_hotel_list()
    {
        $orderby = I('post.orderby');
        $data = $this->_serveObject->gethotelList($orderby);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        //$this->assign('page',$data[1]);
        $this->display('ajax_hotel_list');
    }


    /**
     *增加酒店
     * @Date 201707
     * @Author Lin
     */
    public function hotel_join()
    {
        $fpModel = M('hotel');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                $ret = uploadOne('image', 'hotel', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                $dataPost['add_time'] = time();
                //var_dump($dataPost);die;
                if(false !== $fpModel->add($dataPost))
                {
                    $this->success('增加成功','hotelList',1);
                    exit;
                }

            }
            $this->error($fpModel->getError());
        }
        $this -> display('hotel_join');
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function hoteldetail()
    {
        $id = I('id');
        $hModel = M('hotel');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($hModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'hotel', array(array(300, 300)));

                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功');
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $hModel->where("id=$id")->setInc('number');
        $data = $hModel->find($id);
        //var_dump($data);die;
        $this->assign('data',$data);
        $this->display('hotel_detail');
    }
    /**********************************************酒店end************************************************/


    /**********************************************交通start************************************************/
    /**
     *服务申请列表
     * @Date 201707
     * @Author Lin
     */
    public function trafficList()
    {
        $name =  I('traffic_name');
        $mobile =  I('mobile');
        $status =  I('status');
        $data = $this->_serveObject->gettrafficList($name,$mobile,$status);

       // var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display('traffic_list');
    }

    /**
     *总页数
     * @Date 201707
     * @Author Lin
     */
    public function getTrafficPageCount()
    {
        $map['status'] = 2;
        $count = M('traffic') -> where($map) -> count();
        $Page = ceil($count/5);
        $this->ajaxReturn(array('num' => $Page));
    }

    /**
     *
     * @Date 201707
     * @Author Lin
     */
    public function ajax_traffic_list()
    {
        $orderby = I('post.orderby');
        $data = $this->_serveObject->gettrafficList($orderby);

        //var_dump($data[0]);die;
        $this->assign('data',$data[0]);
        //$this->assign('page',$data[1]);
        $this->display('ajax_traffic_list');
    }





    /**
     *增加
     * @Date 201707
     * @Author Lin
     */
    public function trafficAdd()
    {
        $fpModel = M('traffic');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                $ret = uploadOne('image', 'traffic', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                $dataPost['add_time'] = time();
                //var_dump($dataPost);die;
                if(false !== $fpModel->add($dataPost))
                {
                    $this->success('增加成功','trafficList',1);
                    exit;
                }

            }
            $this->error($fpModel->getError());
        }
        $this -> display();
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function trafficdetail()
    {
        $id = I('id');
        $hModel = M('traffic');
       /* if(IS_POST)
        {
            $dataPost = I('post.');
            if($hModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'traffic', array(array(300, 300)));

                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功','trafficList',1);
                    exit;
                }
            }
            $this->error($hModel->getError());
        }*/
        $data = $hModel->find($id);
        $this->assign('data',$data);
        $this->display('traffic_detail');
    }
    /**********************************************酒店end************************************************/

    /**********************************************基地start************************************************/
    /**
     *服务申请列表
     * @Date 201707
     * @Author Lin
     */
    public function baseList()
    {
        $name =  I('base_name');
        $mobile =  I('mobile');
        $status =  I('status');
        $data = $this->_serveObject->getbaseList($name,$mobile,$status);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    /**
     *增加
     * @Date 201707
     * @Author Lin
     */
    public function baseAdd()
    {
        $fpModel = M('base');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                $ret = uploadOne('image', 'traffic', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                $dataPost['add_time'] = time();
                //var_dump($dataPost);die;
                if(false !== $fpModel->add($dataPost))
                {
                    $this->success('增加成功','baseList',1);
                    exit;
                }

            }
            $this->error($fpModel->getError());
        }
        $this -> display();
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function baseEdit()
    {
        var_dump(session('username'));die;
        if(!session('username'))
        {
            $this->redirect('',U('Login/index'),2);
        }
        $id = I('id');
        $hModel = M('base');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($hModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'base', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功','baseList',1);
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $data = $hModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }
    /**********************************************基地end************************************************/

    /**
     *酒店服务用户信息
     * @Date 201707
     * @Author Lin
     */
    public function userHotelList()
    {
        $mobile =  I('mobile');
        $data = $this->_serveObject->getUserHotelList($mobile);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();

    }

    //提交用户酒店服务
    public function user_hotel()
    {
        $hModel = M('user_hotel');
        $dataPost = I('post.');
        $dataPost['user_id'] = $this->user_id;
        $dataPost['time'] = strtotime($dataPost['time']);
        /*$dataPost['mobile'] = I('mobile');
        $dataPost['hotel_id'] = I('hotel_id');
        $dataPost['room_cateid'] = I('room_cateid');
        $dataPost['room_num'] = I('room_num');
        $dataPost['user_num'] = I('user_num');*/
        $dataPost['add_time'] = time();
        //var_dump($dataPost);die;
        $id = $hModel->add($dataPost);

        //发邮件到服务商邮箱
        $email = M('hotel')->where("id=".I('hotel_id'))->getField('email');
        $username = M('users')->where("id=".$this->user_id)->getField('nickname');
        $room_cattid = I('room_cateid');

        switch ($room_cattid)
        {
            case 1:
                $rootcat = "标准客房";
                break;
            case 2:
                $rootcat = "双人客房";
                break;
            case 3:
                $rootcat = "豪华套房";
                break;
            default:
                $rootcat = "待定";
        }
        //HTML内容转换
        $content = "
                    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
                    <div style='width:50%;margin: 0 auto;padding:30px;border:2px dotted #bd0a01;background:#FFC;'>
                        <P>&nbsp;".iconv('UTF-8','UTF-8',"订单信息如下：")."</P>
                        <P>【姓名】：".iconv('UTF-8','UTF-8',$username)."</P>
                        <P>【电话】：".iconv('UTF-8','UTF-8',I('mobile'))."</P>
                        <P>【房间类型】：".iconv('UTF-8','UTF-8',$rootcat)."</P>
                        <P>【房间数】：".iconv('UTF-8','UTF-8',I('room_num'))."</P>
                        <P>【入住时间】：" . iconv('UTF-8', 'UTF-8', I('time')) . "</P>
                        <P>【入住人数】：".iconv('UTF-8','UTF-8',I('user_num'))."</P>
                        <P>【提交时间】：".iconv('UTF-8','UTF-8',date('Y-m-d H:i:s',time()))."</P>
                    </div>";

        if(send_email($email,'你有新的订单信息',$content))
        {
            if($id)
            {
                $this->ajaxReturn(array('status'=>1,'message'=>'信息提交成功！'));
            }
            $this->ajaxReturn(array('status'=>-1,'message'=>'信息提交失败，请重试！'));
        }
        $this->ajaxReturn(array('status'=>-2,'message'=>'信息提交失败，请重试！'));
    }


    /**
     *基地服务用户信息
     * @Date 201707
     * @Author Lin
     */
    public function userBaseList()
    {
        $mobile =  I('mobile');
        $data = $this->_serveObject->getUserBaselList($mobile);

       // var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    public function userBaseEdit()
    {
        $id = I('id');
        $hModel = M('user_base');
        if(IS_POST)
        {
            $dataPost = I('post.');
            $dataPost['time'] = strtotime($dataPost['time']);
            //var_dump($dataPost);die;
            if($hModel->create($dataPost,2))
            {
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功','userBaseList',1);
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $data = $hModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }




    /**
     *状态展示
     * @Date 201707
     * @Author Lin
     */
    public function statusEdit()
    {
        $Id = I('Id');
        $status = I('status');
        $model = I('model');
        //var_dump($menuId);var_dump($status);die;

        $result = M($model)->where(array('id'=>$Id))->setField('status',$status);

        if ($result !== false)
            $this->ajaxReturn(array('status'=>1));
        else
            $this->ajaxReturn(array('status'=>0));
    }

    /**
     * 单条删除
     */
    public function dataDel()
    {
        if(!IS_AJAX) return;
        $model = I('model');
        $result = M($model)-> delete(intval($_GET['id']));
        if($result)
            $this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
        else
            $this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
    }
    /**
     *全选删除
     * @Date 201703
     * @Author Linsenquan
     */
    public function deleteAll()
    {
        //var_dump($_POST);die;
        if(!IS_POST)
        $this->error('非法操作');
        if(empty($_POST))
            $this->error('请选择要删除的信息！','',1);
       //var_dump($_POST);die;
        $result = $this->_serveObject->deleteAllData($_POST);
        if($result)
            $this->success('删除成功','',1);
        else
            $this->error('删除失败','',1);
    }
    //  hotelList  trafficList


    /**
     * 轮播图
     * @date 2017年7月28日 - 下午2:40:53
     * @author JiangYongWei
     */
    public function benner(){
        $field = array('id', 'link', 'advert_img');
        $benner_data = M('advert')->field($field)->where("media_type = 1 and enabled = 1")->order('sort asc, id desc')->select();
        $this->assign('benner', $benner_data);
    }

    //服务列表
    public function serve_index()
    {
        $this->display('server_index');
    }
    //基地列表
    public function base_index()
    {
        $cat_id = I('cat_id');
        $categoryList =  M("base_category") ->where('is_show=1')->order('sort asc,id asc')->limit(4)->select();//分类
//		var_dump($categoryList);die;

        $base = array();
        foreach ($categoryList as $k => $v)
        {
            $base[] =  M('base')->where('category='.$v['id'])->select();
        }
//        var_dump($base);die;

        $this->assign('categoryList',$categoryList);
        $this->assign('base1',$base[0]);
        $this->assign('base2',$base[1]);
        $this->assign('base3',$base[2]);
        $this->assign('base4',$base[3]);
        self::benner();
        $this->display('base');
    }

    //基地详情
    public function base_detail()
    {
//        var_dump(session('username'));die
        $baseid  = I('base_id');
        M('base')->where("id=$baseid")->setInc('number');
        $data = M('base')->find($baseid);
        $this->assign('data',$data);
        $this->display('base_detail');
    }

    //用户基地服务
    public function user_base()
    {
        $hModel = M('user_base');
        $dataPost = I('post.');
        $dataPost['user_id'] = $this->user_id;
        // dump($this->user_id);
        $dataPost['time'] = strtotime($dataPost['time']);
        $dataPost['mobile'] = I('mobile');
        /*$dataPost['hotel_id'] = I('hotel_id');
        $dataPost['room_cateid'] = I('room_cateid');
        $dataPost['room_num'] = I('room_num');
        $dataPost['user_num'] = I('user_num');
        $dataPost['add_time'] = time();*/
//        var_dump($dataPost);exit;
        $this->_users = new UsersModel();
        //是否为会员
        $user_type = $this->_users->isUsers($this->user_id);
//         dump($user_type);exit;
        if($user_type == 2){
             $this->ajaxReturn(array('status' => 2, 'message' => '对不起，您还不是正式会员，请绑定手机成为会员！'));
        }

        $id = $hModel->add($dataPost);

        //发邮件到服务商邮箱
        $email = M('base')->where("id=" . I('base_id'))->getField('email');
//        var_dump($email);exit;
        $username = M('users')->where("id=" . $this->user_id)->getField('nickname');
        //HTML内容转换
        $content = "
                    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
                    <div style='width:50%;margin: 0 auto;padding:30px;border:2px dotted #bd0a01;background:#FFC;'>
                        <P>&nbsp;" . iconv('UTF-8', 'UTF-8', "订单信息如下：") . "</P>
                        <P>【姓名】：" . iconv('UTF-8', 'UTF-8', $username) . "</P>
                        <P>【电话】：" . iconv('UTF-8', 'UTF-8', I('mobile')) . "</P>
                        <P>【人数】：" . iconv('UTF-8', 'UTF-8', I('user_num')) . "</P>
                        <P>【时间】：" . iconv('UTF-8', 'UTF-8', I('time')) . "</P>
                        <P>【备注】：" . iconv('UTF-8', 'UTF-8', I('remark')) . "</P>
                        <P>【提交时间】：" . iconv('UTF-8', 'UTF-8', date('Y-m-d H:i:s', time())) . "</P>
                    </div>";

        if (send_email($email, '你有新的订单信息', $content))
        {
            if ($id) {
                $this->ajaxReturn(array('status' => 1, 'message' => '信息提交成功！'));
            }
            $this->ajaxReturn(array('status' => -1, 'message' => '信息提交失败，请重试！'));
        }
        $this->ajaxReturn(array('status' => -2, 'message' => '信息提交失败，请重试！'));

    }

}